package com.tanhua.app.service;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.api.db.QuestionApi;
import com.tanhua.api.db.SettingsApi;
import com.tanhua.api.db.BlackListApi;
import com.tanhua.api.db.UserApi;
import com.tanhua.domain.db.Question;
import com.tanhua.domain.db.Settings;
import com.tanhua.domain.db.User;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.vo.PageBeanVo;
import com.tanhua.domain.vo.SettingsVo;
import com.tanhua.domain.vo.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.time.Duration;


@Service
public class SettingsService {

    @Reference
    private QuestionApi questionApi;

    @Reference
    private SettingsApi settingsApi;

    @Reference
    private BlackListApi blackListApi;

    @Reference
    private UserApi userApi;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    //查询交友问题并展示
    public ResponseEntity findSettings() {
        User user = UserHolder.getUser();
        //根据id查询问题
        Question question = questionApi.findQuestion(user.getId());

        //根据id查询通知对象
        Settings settings = settingsApi.findSettings(user.getId());

        //将问题和通知设置给SettingsVo
        SettingsVo settingsVo = new SettingsVo();
        if(question!=null){
            settingsVo.setStrangerQuestion(question.getStrangerQuestion());
        }
        if(settings!=null){
            settingsVo.setLikeNotification(settings.getLikeNotification());
            settingsVo.setPinglunNotification(settings.getPinglunNotification());
            settingsVo.setGonggaoNotification(settings.getGonggaoNotification());
        }
        settingsVo.setPhone(user.getPhone());
        settingsVo.setId(user.getId());

        // 返回settingsVo给app
        return ResponseEntity.ok(settingsVo);
    }


    //保存交友问题
    public ResponseEntity saveOrUpdateQuestions(String content) {
        // 根据id查询问题
        Long userId = UserHolder.getUser().getId();
        Question question = questionApi.findQuestion(userId);
        if(question==null){
            // 没有-新增
            question=new Question();
            question.setStrangerQuestion(content);
            question.setUserId(userId);
            questionApi.save(question);
        }else{
            // 有-修改
            question.setStrangerQuestion(content);
            questionApi.update(question);
        }
        return ResponseEntity.ok(null);
    }


    //保存更新通知
    public ResponseEntity saveOrUpdateTZ(Settings settings) {
        // 根据id查询通知
        Long userId = UserHolder.getUser().getId();
        Settings st = settingsApi.findSettings(userId);
        if(st==null){
            // 没有-新增
            settings.setUserId(userId);
            settingsApi.save(settings);
        }else{
            // 有-修改
            st.setPinglunNotification(settings.getPinglunNotification());
            st.setLikeNotification(settings.getLikeNotification());
            st.setGonggaoNotification(settings.getGonggaoNotification());
            settingsApi.update(st);
        }
        return ResponseEntity.ok(null);
    }


    public ResponseEntity findBlackList(Integer page, Integer pagesize) {
        //分页查询
        IPage<UserInfo> iPage = blackListApi.findBlackList(page, pagesize, UserHolder.getUser().getId());
        //将iPage的内容转换成前端要的pageBean
        PageBeanVo pageBeanVo = new PageBeanVo(page,pagesize,iPage.getTotal(),iPage.getRecords());
        return ResponseEntity.ok(pageBeanVo);
    }

    //***

    public ResponseEntity deleteBlackById(Integer blackId, Long userId) {
        blackListApi.deleteBlackById(blackId,userId);

        return ResponseEntity.ok(null);
    }


    public ResponseEntity savePhone(String phone) {
        Long id = UserHolder.getUser().getId();
        User user = userApi.findPhoneById(id);
        user.setPhone(phone);
        userApi.updatePhone(user);
        return ResponseEntity.ok(null);
    }


    public ResponseEntity sendVerificationCode(String phone){
        //发短信
        String code = RandomUtil.randomNumbers(6);

/*
        smsTemplate.sendSms(phone,code);
*/
        //验证码放入redis中
        //String code="123456";
        stringRedisTemplate.opsForValue().set("SMS_CODE_"+phone,code, Duration.ofMinutes(5));

        System.out.println("验证码: "+code);
        return ResponseEntity.ok(null);
    }

    public ResponseEntity changeVerification(String verificationCode) {
        //2 就页面用户填写的验证码和redis的验证码对比
        boolean v;
        String code = stringRedisTemplate.opsForValue().get("V_CODE_" + verificationCode);
        if (StrUtil.equals(verificationCode, code)) {
            //一致
            v = true;
            //查找成功删除验证码
            stringRedisTemplate.delete("SMS_CODE_" + verificationCode);

        } else {
            v = false;
            return ResponseEntity.status(500).body("验证码错误");
        }
        return ResponseEntity.ok(v);
    }
}
